<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ZTree DEMO</title>
<link rel="stylesheet" type="text/css" href="/css/zTreeStyle/zTreeStyle.css"  />
</head>
<body>


<div id="treeDemo" class="ztree"></div>

<div>
<button class="btn" onclick="ttt('idInput','nameInput')">获取树选中的数据</button>
<input id="idInput" >
<input id="nameInput" >
</div>
<script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/js/jquery.ztree.all.min.js"></script>
<script type="text/javascript">
	var zTreeObj;
	// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
	var setting = {
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: 0
			}
		},
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "s", "N": "s" }
		}
	};
	// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
// 	var zNodes = [
// 	{name:"test1", open:true, children:[
// 	   {name:"test1_1"}, {name:"test1_2"}]},
// 	{name:"test2", open:true, children:[
// 	   {name:"test2_1"}, {name:"test2_2"}]}
// 	];
	var zNodes = [
    {"id":1, "pId":0, "name":"test1",open:true},
    {"id":11, "pId":1, "name":"test11"},
    {"id":12, "pId":1, "name":"test12"},
    {"id":111, "pId":11, "name":"test111"}
	];
	$(function(){
		zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
	});
	
	function ttt(id,name){
// 		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
// 		var nodes = treeObj.getSelectedNodes();
// 		var nodes = treeObj.getCheckedNodes(true);
		var nodes = zTreeObj.getCheckedNodes(true);
		console.log(nodes);
		var ids = "";
		var names = "";
		for(var i in nodes){
			if(ids ==""){
				ids += nodes[i].id;
				names += nodes[i].name;
			}else{
				ids += ","+nodes[i].id;
				names += ","+nodes[i].name;
			}
		}
		$("#"+id).val(ids);
		$("#"+name).val(names);
	}
</script>
</body>
</html>